<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property integer $role
 * @property integer $country_id
 * @property double $start_currency
 * @property double $current_currency
 * @property double $material_inventory
 * @property double $products_inventory
 * @property double $total_loan
 * @property double $previous_netasset
 * @property double $current_netasset
 * @property double $netasset_growth
 * @property integer $material_level
 * @property double $tech_level
 * @property string $create_at
 * @property string $update_at
 *
 * @property Loan[] $loans
 * @property Member[] $members
 * @property News[] $news
 * @property Order[] $orders
 * @property Order[] $orders0
 * @property Product[] $products
 * @property Transfer[] $transfers
 * @property Transfer[] $transfers0
 * @property Country $country
 * @property UserLog[] $userLogs
 * @property UserMaterial[] $userMaterials
 * @property UserProduct[] $userProducts
 * @property string $roleStr
 */
class User extends \app\models\BaseModel {
    const ROLE_OTHER = 0;
    const ROLE_PRODUCER = 1;
    const ROLE_TRADER = 2;
    const ROLE_GOV = 3;
    const ROLE_ADMIN = 4;
    public static $roles = [
        self::ROLE_OTHER => '其他',
        self::ROLE_PRODUCER => '生产商',
        self::ROLE_TRADER => '贸易商',
        self::ROLE_GOV => '政府',
        self::ROLE_ADMIN => '组委会',
    ];
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['username', 'password', 'role'], 'required'],
            [['role', 'country_id', 'material_level'], 'integer'],
            [['start_currency', 'current_currency', 'material_inventory', 'products_inventory', 'total_loan', 'previous_netasset', 'current_netasset', 'netasset_growth', 'tech_level'], 'number'],
            [['create_at', 'update_at'], 'safe'],
            [['username', 'password'], 'string', 'max' => 255],
            [['username'], 'unique'],
            [['country_id'], 'exist', 'skipOnError' => true, 'targetClass' => Country::className(), 'targetAttribute' => ['country_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'username' => '用户名',
            'password' => '密码',
            'role' => '角色（生产商、贸易商、政府、组委会）',
            'country_id' => '国家',
            'start_currency' => '起始资金',
            'current_currency' => '当前资金',
            'material_inventory' => '原料库存',
            'products_inventory' => '产品库存',
            'total_loan' => '贷款总额',
            'previous_netasset' => '上一财年净资产',
            'current_netasset' => '本财年净资产',
            'netasset_growth' => '净资产增长率',
            'material_level' => '原料水晶等级',
            'tech_level' => '科技值',
            'create_at' => '创建时间',
            'update_at' => '更新时间',
        ];
    }

    public function fields() {
        $fields = [
            'id', 'role', 'username', 'material_level', 'current_currency',

            'start_currency', 'material_inventory', 'products_inventory', 'total_loan',
            'previous_netasset', 'current_netasset', 'netasset_growth', 'tech_level',

            'country',
        ];
        $fields = array_combine($fields, $fields);

        return $fields;
    }

    public function extraFields() {
        $fields = [
            'password', 'country_id',
            'create_at', 'update_at',
        ];
        $fields = array_combine($fields, $fields);
        return $fields;
    }

    public function getRoleStr() {
        return self::$roles[$this->role];
    }

    public function getLoans() {
        return $this->hasMany(Loan::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    public function getMembers() {
        return $this->hasMany(Member::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    public function getNews() {
        return $this->hasMany(News::className(), ['author_id' => 'id'])->inverseOf('author');
    }

    public function getOrders() {
        return $this->hasMany(Order::className(), ['provider_id' => 'id'])->inverseOf('provider');
    }

    public function getOrders0() {
        return $this->hasMany(Order::className(), ['trader_id' => 'id'])->inverseOf('trader');
    }

    public function getProducts() {
        return $this->hasMany(Product::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    public function getTransfers() {
        return $this->hasMany(Transfer::className(), ['income_user_id' => 'id'])->inverseOf('incomeUser');
    }

    public function getTransfers0() {
        return $this->hasMany(Transfer::className(), ['transfer_user_id' => 'id'])->inverseOf('transferUser');
    }

    public function getCountry() {
        return $this->hasOne(Country::className(), ['id' => 'country_id'])->inverseOf('users');
    }

    public function getUserLogs() {
        return $this->hasMany(UserLog::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    public function getUserMaterials() {
        return $this->hasMany(UserMaterial::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    public function getUserProducts() {
        return $this->hasMany(UserProduct::className(), ['user_id' => 'id'])->inverseOf('user');
    }
}
